package com.cz.fra.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONArray;
import org.json.JSONObject;

import com.cz.fra.entity.Comment;
import com.cz.fra.service.CommentService;
import com.cz.fra.service.ServiceException;
import com.cz.fra.service.impl.CommentServiceImpl;

/**
 * Retrieve all those reports from database for the first time login.
 * 
 * @author victor
 *
 */
public class RetreiveReportDetailsServlet extends HttpServlet{

	 /**
	 * Generated serialVersionUID
	 */
	private static final long serialVersionUID = 1L;
	
	private CommentService commentService = new CommentServiceImpl();
	
	public void doGet(HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException{
		int reportID = Integer.parseInt(request.getParameter("reportId").trim());
	    try {
			List<Comment> commentList = commentService.getCommentListByReportID(reportID);
			PrintWriter out = response.getWriter();
		     JSONArray array = new JSONArray();
		     for(Comment c : commentList){
		    	 JSONObject obj = new JSONObject();
		    	 try{
		    		 obj.put("commentId", c.getCommentId());
		    		 obj.put("reportId", c.getReportId());
		    		 obj.put("content", c.getContent());
		    		 obj.put("commentBy", c.getCommentBy());
		    		 obj.put("commentDate", c.getCommentDate());
		    	 }catch(Exception e){
		    		 e.printStackTrace();
		    	 }
		    	 array.put(obj);
		     }
		     out.write(array.toString());
		     out.flush();
		     out.close();
		} catch (ServiceException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	    //set the comment list into reponse return to clients.
	    //TODO
	}
	
	public void doPost(HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException{
		doGet(request,response);
	}
	
}
